VFG.EPolyline = function(viewer, options) {
	this.viewer = viewer;
	this.options = options;
	this.parentId = options.parentId || '';
	this.id = options.id;
	this.name = options.name || '';
	this.code = options.code || '';
	this.type = options.type || '';
	this.thing = options.thing || {};
	this.attribute = options.attribute || {};
	this.propertys = options.propertys || [];
	this.init();
}
VFG.EPolyline.prototype.init = function() {
	let _this = this;
	this.entity = this.viewer.entities.add({
		id : this.id,
		name : this.name,
		code : this.code,
		type : this.type,
		polyline : {
			positions : this.thing.positions,
			material : this.getMaterial(),
			arcType : this.getArcType(),
			width : this.thing.width || 1,
			classificationType : this.getClassificationType(),
			distanceDisplayCondition : this.getDistanceDisplayCondition(),
		}
	});
};

VFG.EPolyline.prototype.updateWidth = function(width) {
	let _this = this;
	this.thing.width=width;
	if(this.entity){
		this.entity.polyline.width=this.thing.width;
	}
};

VFG.EPolyline.prototype.updateName = function(name) {
	let _this = this;
	this.name=name;
};

VFG.EPolyline.prototype.updateCode = function(code) {
	let _this = this;
	this.code=code;
};

VFG.EPolyline.prototype.getMaterial = function() {
	if ('line' == this.thing.materialType) {
		return this.thing.color ? Cesium.Color
				.fromCssColorString(this.thing.color) : Cesium.Color.RED
	} else if ('glow' == this.thing.materialType) {
		return new Cesium.PolylineGlowMaterialProperty({
			glowPower : this.thing.glowPower || 0.2,
			taperPower : this.thing.taperPower || 0.5,
			color : this.thing.color ? Cesium.Color
					.fromCssColorString(this.thing.color) : Cesium.Color.RED,
		})
	} else if ('outline' == this.thing.materialType) {
		return new Cesium.PolylineOutlineMaterialProperty({
			color : this.thing.color ? Cesium.Color
					.fromCssColorString(this.thing.color) : Cesium.Color.RED,
			outlineWidth : this.thing.outlineWidth || 2,
			outlineColor : this.thing.outlineColor ? Cesium.Color
					.fromCssColorString(this.thing.outlineColor)
					: Cesium.Color.BLACK,
		})
	} else if ('dash' == this.thing.materialType) {
		return new Cesium.PolylineDashMaterialProperty({
			color : this.thing.color ? Cesium.Color
					.fromCssColorString(this.thing.color) : Cesium.Color.RED,
			dashLength:this.thing.dashLength||16.0,
			dashPattern:this.thing.dashPattern||255.0,
			gapColor:this.thing.gapColor ? Cesium.gapColor
					.fromCssColorString(this.thing.gapColor) : Cesium.Color.TRANSPARENT
		})
	} else if ('arrow' == this.thing.materialType) {
		return new Cesium.PolylineArrowMaterialProperty(
				this.thing.color ? Cesium.Color
						.fromCssColorString(this.thing.color)
						: Cesium.Color.RED);
	} else {
		return this.thing.color ? Cesium.Color
				.fromCssColorString(this.thing.color) : Cesium.Color.RED
	}
}

VFG.EPolyline.prototype.updatePositions = function(positions, isDynamic) {
	let _this = this;
	if (positions) {
		this.thing.positions = positions;
		if (this.entity) {
			if (isDynamic) {
				if(!(this.entity.polyline.positions instanceof Cesium.CallbackProperty)){
					this.entity.polyline.positions = new Cesium.CallbackProperty(
						function(time) {
							return _this.thing.positions
						}, false)
				}
			} else {
				this.entity.polyline.positions = _this.thing.positions
			}
		}
	}
}

/**
 * clampToGround
 */
VFG.EPolyline.prototype.updateClampToGround = function(clampToGround) {
	let _this = this;
	if (clampToGround) {
		this.thing.clampToGround = clampToGround;
		if (this.entity) {
			if ('1' == this.thing.clampToGround) {
				this.entity.polyline.clampToGround = true
			} else {
				this.entity.polyline.clampToGround = false
			}
		}
	}
}

VFG.EPolyline.prototype.getArcType = function() {
	if ('GEODESIC' == this.thing.arcType) {
		return Cesium.ArcType.GEODESIC;
	} else if ('NONE' == this.thing.arcType) {
		return Cesium.ArcType.NONE;
	} else if ('RHUMB' == this.thing.arcType) {
		return Cesium.ArcType.RHUMB;
	} else {
		return Cesium.ArcType.GEODESIC;
	}
}

VFG.EPolyline.prototype.getClassificationType = function() {
	if ('BOTH' == this.thing.classificationType) {
		return Cesium.ClassificationType.BOTH;
	} else if ('CESIUM_3D_TILE' == this.thing.classificationType) {
		return Cesium.ClassificationType.CESIUM_3D_TILE;
	} else if ('TERRAIN' == this.thing.classificationType) {
		return Cesium.ClassificationType.TERRAIN;
	} else {
		return Cesium.ClassificationType.BOTH;
	}
}

VFG.EPolyline.prototype.getDistanceDisplayCondition = function() {
	if('1'==this.thing.isDistanceDisplay){
		if (this.thing.distanceDisplayNear && this.thing.distanceDisplayFar) {
			return new Cesium.DistanceDisplayCondition(
					this.thing.distanceDisplayNear || 0.0,
					this.thing.distanceDisplayFar || Number.MAX_VALUE);
		}
	}
	return undefined
}

VFG.EPolyline.prototype.updateMaterial = function(material) {
	
	if(material.materialType){
		this.thing.materialType=material.materialType;
	}
	if(material.color){
		this.thing.color=material.color;
	}
	if(material.glowPower){
		this.thing.glowPower=material.glowPower;
	}
	if(material.taperPower){
		this.thing.taperPower=material.taperPower;
	}
	if(material.outlineWidth){
		this.thing.outlineWidth=material.outlineWidth;
	}
	if(material.outlineColor){
		this.thing.outlineColor=material.outlineColor;
	}
	if(material.outlineColor){
		this.thing.outlineColor=material.outlineColor;
	}
	if(material.dashLength){
		this.thing.dashLength=material.dashLength;
	}
	if(material.dashPattern){
		this.thing.dashPattern=material.dashPattern;
	}
	if(material.gapColor){
		this.thing.gapColor=material.gapColor;
	}
	if(this.entity){
		this.entity.polyline.material=this.getMaterial();
	}
}

VFG.EPolyline.prototype.updateClassificationType = function(classificationType) {
	this.thing.classificationType=classificationType;
	if(this.entity){
		this.entity.polyline.classificationType=this.getClassificationType();
	}
}

VFG.EPolyline.prototype.updateArcType = function(arcType) {
	this.thing.arcType=arcType;
	if(this.entity){
		this.entity.polyline.arcType=this.getArcType();
	}
}

VFG.EPolyline.prototype.updateDistanceDisplayCondition = function(near,far) {
	this.thing.arcType=arcType;
	if(near){
		this.thing.distanceDisplayNear=near;
	}
	if(far){
		this.thing.distanceDisplayFar=far;
	}
	if(this.entity){
		if('1'==this.thing.isDistanceDisplay){
			this.entity.polyline.distanceDisplayCondition=this.getDistanceDisplayCondition();
		}
	}
}

VFG.EPolyline.prototype.isDistanceDisplay = function(isDistanceDisplay) {
	this.thing.isDistanceDisplay=isDistanceDisplay;
	if(this.entity){
		if('1'==this.thing.isDistanceDisplay){
			this.entity.polyline.distanceDisplayCondition=this.getDistanceDisplayCondition();
		}else{
			this.entity.polyline.distanceDisplayCondition=undefined;
		}
	}
}


VFG.EPolyline.prototype.updateProperty= function(property) {
	var _this = this;
	if(_this.propertys){
		for(let index in _this.propertys){
			if(property.id==_this.propertys[index].id){
				_this.propertys.splice(index,1,property);
				return;
			}
		}
		_this.propertys.push(property);
	}
};

VFG.EPolyline.prototype.removeProperty= function(id) {
	var _this = this;
	if(_this.propertys){
		for(let index in _this.propertys){
			if(id==_this.propertys[index].id){
				_this.propertys.splice(index,1);
				return;
			}
		}
	}
};

VFG.EPolyline.prototype.destroy = function() {
	let _this = this;
	if (this.entity) {
		this.viewer.entities.remove(this.entity);
	}
	delete this.entity
	delete this.options
	delete this.thing
	delete this.viewer
	return Cesium.destroyObject(this);
};
